package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.rdb.api.RoleAuthorityRdbService;
import com.ndp.fb.rdb.model.RoleAuthority;
import com.ndp.fb.redis.api.RedisClient;
import com.ndp.fb.walle.business.RoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by elvisxiong on 7/9/15.
 */
@Component
public class RoleAuthorityServiceImpl implements RoleAuthorityService {

    @Reference
    private RoleAuthorityRdbService roleAuthorityRdbService;

    @Autowired
    private  RedisClient redisClient;

    @Override
    public List<RoleAuthority> getAuthoritysByRole(List<Long> roleIds) {
        StringBuffer roles = new StringBuffer();
        for(int i=0 ; i<roleIds.size();i++)
        {
            roles.append(roleIds.get(i));
            roles.append("$");
        }
        Object roleAuthorityObj = redisClient.get(CommonConstants.USER_ROLE_AUTHORITY + roles);
        List<RoleAuthority> roleAuthorityList = null;
        if(roleAuthorityObj !=null){
            roleAuthorityList = (List<RoleAuthority>)roleAuthorityObj;
            if(roleAuthorityList.size() >0){
                return roleAuthorityList;
            }

        }
        roleAuthorityList =  roleAuthorityRdbService.getRoleAuthorityByRole(roleIds);
        redisClient.set(CommonConstants.USER_ROLE_AUTHORITY + roles,roleAuthorityList,1, TimeUnit.HOURS.toString());
        return roleAuthorityList;
    }
}
